//
//  HTTPTool.m
//  MyDIYHouse
//
//  Created by dlios on 15-6-29.
//  Copyright (c) 2015年 李平洋. All rights reserved.
//

#import "HTTPTool.h"
#import "AFNetworking.h"

@implementation HTTPTool

+(void)get:(NSString *)url
      body:(id)body
  httpResult :(HTTPStyle)style
   success:(void(^)(id result))success
   failure:(void(^)(NSError *error))failure
{
    //获取AFN网络请求的管理类
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    
    //判断返回的responseObject的数据类型
    switch (style) {
        case HTTP:
            //NSData类型
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case JSON:
            //JSON类型;
            break;
        case XML:
            //XML 类型
             manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            
        default:
            break;
    }
    
     NSString *urlStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    //让管理类调用get请求
    [manager GET:urlStr parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        //如果成功返回数据
        if (success) {
              success(responseObject);
        }
      
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //如果失败,返回error信息
        if(failure){
              failure(error);
        }
        }];
    
    
}

+(void)post:(NSString *)url
       body:(id)body
httpResult :(HTTPStyle)style
requestBodyStyle:(RequestBodyStyle)bodyStyle
    success:(void(^)(id result))success
    failure:(void(^)(NSError *error))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    
    switch (bodyStyle) {
        case BodyHTTP:
            manager.requestSerializer = [AFHTTPRequestSerializer serializer];
            break;
         case BodyJSON:
            manager.requestSerializer = [AFJSONRequestSerializer serializer];
            break;

        default:
            break;
    }
    
    

    
    switch (style) {
        case HTTP:
            //NSData类型
            manager.responseSerializer = [AFHTTPResponseSerializer serializer];
            break;
        case JSON:
            //JSON类型;
            break;
        case XML:
            //XML 类型
            manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
            
        default:
            break;
    }

    
     NSString *urlStr = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [manager POST:urlStr parameters:body success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (failure) {
            failure(error);
        }
    }];



}


@end
